<?php
namespace app\api\controller;

use app\BaseController;
use app\common\model\mysql\CommonUser as models;
use app\common\model\mysql\CommonUser;
use app\common\model\mysql\CommonSysOtherConfig;
use app\common\model\mysql\CommonSysSmsSendRecord;
use app\common\model\mysql\YwStaff;
use think\facade\Db;

class Login extends BaseController
{

    public function initialize()
    {
    }

    /*
     * 前台登录
     */
    public function login(){
        $fildes = 'username,password';
        $receiveData = getData($this->request->post(),$fildes);
        if(empty($receiveData['username']) || empty($receiveData['password'])){
            jsonFail('用户名或密码不能为空');
        }
        $load = models::where(['username'=>$receiveData['username'],'password'=>pwdMd5($receiveData['password'])])->find();
        if(empty($load)){
            jsonFail('用户名或密码错误');
        }
        if($load['status'] != 1){
            jsonFail('该用户已禁用');
        }
        $token = api_token($load['id']);
        models::where(['id'=>$load['id']])->update([
            'token'=>$token,
            'token_flush_at'=>date('Y-m-d H:i:s'),
            'last_login_at' => date('Y-m-d H:i:s'),
            'last_login_ip' => get_server_ip()
        ]);
        $data = [
            'token' => $token
        ];
        jsonSuccess($data);
    }

    /*
     * 获取动态码
     */
    public function getCode(){
        $fildes = 'mobile';
        $receiveData = getData($this->request->post(),$fildes);
        if(empty($receiveData['mobile'])){
            jsonFail('手机号码不能为空');
        }
        if(!isCellphone($receiveData['mobile'])){
           jsonFail('手机号码不正确，请重新输入');
        }
        $code = generateStr(6,'0123456789');
        CommonSysSmsSendRecord::insertSmsSendRecord($receiveData['mobile'],$code);
        $data = [
            'code' => $code
        ];
        jsonSuccess($data);
    }

    /*
     * 注册
     */
    public function register(){
        $fildes = 'username,password,invite_code,code';
        $receiveData = getData($this->request->post(),$fildes);
        $receiveData['username'] = trim($receiveData['username']);
        if(empty($receiveData['username'])){
            jsonFail('请输入用户名');
        }
        if(empty($receiveData['password'])){
            jsonFail('请输入密码');
        }
        if(!check_str($receiveData['username'])){
           jsonFail('用户名不能包含特殊字符和汉字，只能包含，数字，字母大小写');
        }
        if(strlen($receiveData['username']) < 6){
            jsonFail('用户名不能少于6位，请重新设置');
        }
        if(strlen($receiveData['password']) < 6){
            jsonFail('密码不能少于6位，请重新设置');
        }
        $load = models::where(['username'=>$receiveData['username']])->find();
        if(!empty($load)){
            jsonFail('该用户名已注册，不能重复注册');
        }
        if(empty($receiveData['invite_code'])){
            jsonFail('请输入邀请码');
        }
        $otherConfig = CommonSysOtherConfig::where(['id'=>1])->find();
        if($receiveData['invite_code'] == $otherConfig['basic_invite_code']){
            $staff_id = 0;
        }else{
            $invite_code = $receiveData['invite_code'];
            $where = [];
            $where[] = ['is_delete','=',0];
            $where[] = ['status','=',1];
            $sql_category = "FIND_IN_SET('{$invite_code}',extend_str)";
            $where[] = ['','exp',Db::raw($sql_category)];
            $staff = YwStaff::where($where)->find();
            if(empty($staff)){
                jsonFail('请输入正确的邀请码');
            }
            $staff_id = $staff['id'];
        }
        $insertData = [
            'username' => $receiveData['username'],
            'password' => pwdMd5($receiveData['password']),
            'create_time' => date('Y-m-d H:i:s'),
            'invite_code' => $receiveData['invite_code'],
            'staff_id' => $staff_id
        ];
        models::insert($insertData);
        jsonSuccess();
    }

    /*
     * 忘记密码
     */
    public function forgetPwd(){
        $fildes = 'username,code,password';
        $receiveData = getData($this->request->post(),$fildes);
        if(empty($receiveData['username'])){
            jsonFail('用户名不能为空');
        }
        if(empty($receiveData['password'])){
            jsonFail('请输入密码');
        }
        if(strlen($receiveData['password']) < 6){
            jsonFail('密码不能少于6位，请重新设置');
        }
        $load = models::where(['username'=>$receiveData['username']])->find();
        if(empty($load)){
            jsonFail('该账号不存在');
        }
        models::where(['id'=>$load['id']])->update([
            'password' => pwdMd5($receiveData['password'])
        ]);
        jsonSuccess();
    }

    /*
    * 退出登录
    */
    public function loyout(){
        $fildes = 'token';
        $receiveData = getData($this->request->post(),$fildes);
        //校验token
        $admin = CommonUser::checkToken($receiveData['token']);
        CommonUser::where(['id'=>$admin['id']])->update([
            'token' => ''
        ]);
        jsonSuccess();
    }

}
